package copyengine.ui.sound
{
	import copyengine.resource.file.sound.CESound;
	import copyengine.utils.ResUtils;
	import copyengine.utils.debug.CELog;

	public final class CESoundManger
	{
		private static var _instance:CESoundManger;

		public static function get instance():CESoundManger
		{
			if (_instance == null)
			{
				_instance = new CESoundManger();
			}
			return _instance;
		}

		private var isStarted:Boolean = false;
		private var soundWarpPackageVector:Vector.<CESoundWarpPackage>;
		private var soundInitPackageVector:Vector.<CESoundInitPackage>;

		public function CESoundManger()
		{
		}

		public function initialize(_soundInitPackageVector:Vector.<CESoundInitPackage>):void
		{
			soundInitPackageVector = _soundInitPackageVector;
		}

		/**
		 *调用该函数后系统开始初始化，load相应资源
		 * 这样可以延迟加载资源，不占用带宽
		 */
		public function start():void
		{
			if (!isStarted)
			{
				isStarted = true;
				doInitialize();
			}
		}

		private function doInitialize():void
		{
			soundWarpPackageVector = new Vector.<CESoundWarpPackage>();
			for each (var initPackage:CESoundInitPackage in soundInitPackageVector)
			{
				var warpPackage:CESoundWarpPackage = new CESoundWarpPackage();
				warpPackage.soundInitPackage = initPackage;
				warpPackage.currentSoundVector = new Vector.<CESound>();
				for (var i:int = 0; i < initPackage.soundChannelNum; i++)
				{
					var sound:CESound = ResUtils.getSound(initPackage.soundFileName);
					warpPackage.currentSoundVector.push(sound);
				}
				soundWarpPackageVector.push(warpPackage);
			}
		}

		public function play(_soundFileName:String):void
		{
			if (isStarted)
			{
				var soundWarpPackage:CESoundWarpPackage = getSoundWarpPackageByFileName(_soundFileName);
				if (soundWarpPackage)
				{
					for each (var sound:CESound in soundWarpPackage.currentSoundVector)
					{
						//如果是没有加载完成就不播放并且现在也没加载完成
						if (!soundWarpPackage.soundInitPackage.isPlayEvenNotLoad && !sound.isSoundLoaded())
						{
							continue;
						}
						else if (sound.isSoundIdle())
						{
							//寻找一个空闲的声道播放当前声音,如果所有声道都不空闲则忽略当前播放请求
							sound.play(soundWarpPackage.soundInitPackage.loopNum, soundWarpPackage.soundInitPackage.isPlayEvenNotLoad);
							break;
						}
					}
				}
			}
		}

		public function stop(_soundFileName:String):void
		{
			if (isStarted)
			{
				var soundWarpPackage:CESoundWarpPackage = getSoundWarpPackageByFileName(_soundFileName);
				if (soundWarpPackage)
				{
					for each (var sound:CESound in soundWarpPackage.currentSoundVector)
					{
						sound.stop();
					}
				}
			}
		}

		private function getSoundWarpPackageByFileName(_soundFileName:String):CESoundWarpPackage
		{
			for each (var soundPackage:CESoundWarpPackage in soundWarpPackageVector)
			{
				if (soundPackage.soundInitPackage.soundFileName == _soundFileName)
				{
					return soundPackage;
				}
			}
			CELog.err("can not find soundWarpPackage by fileName : " + _soundFileName);
			return null;
		}

	}
}
